package com.yxp423.ssmempl.controller;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxp423.ssmempl.bean.Dept;
import com.yxp423.ssmempl.bean.Func;
import com.yxp423.ssmempl.bean.User;
import com.yxp423.ssmempl.bean.dto.UserOnline;
import com.yxp423.ssmempl.service.*;
import com.yxp423.ssmempl.service.impl.RoleServiceImpl;
import com.yxp423.ssmempl.utils.base.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: java类作用描述
 * @Author: yxp
 * @CreateDate: 2019-04-25 15:20
 * @Version: 1.0
 */

@Controller
@RequestMapping("/system")
@Api(tags = "系统设置模块")
public class SystemController {

	@Autowired
	private IUserService userService;
	@Autowired
	private IFuncService funcService;
	@Autowired
	private ISysLogService sysLogService;
	@Autowired
	IRoleService roleService;
	@Autowired
	ISessionService sessionService;

	Cache<String, User> userCache = CacheUtil.newLRUCache(10, DateUnit.MINUTE.getMillis() * 10);

	@ApiOperation("返回添加用户页面 ")
	@RequestMapping("/adduser")
	public String addUser() {


		return "system/adduser";
	}

	@GetMapping("/syslogs")
	public String toSysLogPage() {


		return "system/sys_logs";

	}

	@GetMapping("/rolemanage")
	public String toRoleManage() {
		return "system/role_manage";
	}


	@GetMapping("/funcmana")
	@ApiOperation("返回菜单管理页面")
	public String funcManage() {

		return "system/funcmana";
	}
	@GetMapping("/onlineuser")
	public String toOnlineUser(){

		return "system/online_user";
	}


	@ApiOperation(value = "验证登录名是否存在", notes = "展示首页信息")
	@GetMapping("/checkUserAccount")
	@ResponseBody
	public AjaxResult checkUserAccount(String userAccount) {

		AjaxResult result = new AjaxResult();
		User user = userService.getUserByAccount(userAccount);

		if (user == null) {
			result.setSuccess(true);
			result.setMsg("用户名可用");
		} else {
			result.setSuccess(false);
			result.setMsg("该用户名已存在");
		}


		return result;
	}


	@GetMapping("/saveUserInfo")
	@ResponseBody
	@ApiOperation("保存用户")
	public AjaxResult saveUserInfo(User user, String provinceId, String cityId, String countryId) {
		AjaxResult result = new AjaxResult();

		User currentUser = (User) SecurityUtils.getSubject().getPrincipal();

		if (currentUser == null) {

			result.setSuccess(false);
			result.setMsg("你没有操作权限,请登录");
			return result;
		}

		result = userService.saveUserInfo(user, provinceId, cityId, countryId);


		return result;
	}


	@GetMapping("/addFunc")
	@ResponseBody
	@ApiOperation("添加菜单")
	public AjaxResult addFunc(Func func) {
		AjaxResult result = new AjaxResult();

		result = funcService.saveFunc(func);
		return result;
	}

	@GetMapping("/delFunc")
	@ResponseBody
	public AjaxResult delFunc(Func func) {

		return funcService.deleteFunc(func);
	}


	@GetMapping("/getsyslogs")
	@ResponseBody
	public AjaxResult getSysLogs(Page page) {


		return sysLogService.getSysLogs(page);
	}

	@GetMapping("/getuserdetail")
	@ResponseBody
	public AjaxResult getUserDetail(User user) {

		AjaxResult result = new AjaxResult();

		try {

			if (ObjectUtil.isNotNull(user)) {

				User userCached = userCache.get("user" + user.getUserId());
				if (ObjectUtil.isNull(userCached)) {


					User userById = userService.getUserById(user.getUserId());

					if (ObjectUtil.isNotNull(userById)) {

						userById.setPassword(null);
						userById.setUpdateTime(null);
						userById.setCreatTime(null);
						String idCard = userById.getIdCard();
						String preStr = StrUtil.sub(idCard, 0, 6);
						String fixStr = StrUtil.sub(idCard, 14, 18);
						String newIdCard = preStr + "********" + fixStr;
						userById.setIdCard(newIdCard);

						userById.setAddress(null);
						result.setSuccess(true);
						result.setMsg("成功");
						result.setData(userById);

						userCache.put("user"+user.getUserId(),userById);
						return result;
					}
				}else {

					result.setSuccess(true);
					result.setMsg("成功");
					result.setData(userCached);

				}
			} else {
				result.setSuccess(false);
				result.setMsg("传入数据为空");
				return result;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}


		return result;
	}

	@GetMapping("/saveUserRole")
	@ResponseBody
	public AjaxResult saveUserRole(User user, String roles){

		AjaxResult result = roleService.saveUserRoles(user, roles);

		return result;
	}

	@GetMapping("/resetPassword")
	@ResponseBody
	public AjaxResult resetPassword(User user){

		return userService.resetUserPassword(user);
	}

	@GetMapping("/getuseronline")
	@ResponseBody
	public AjaxResult getUserOnline(HttpServletRequest request){

		AjaxResult result = new AjaxResult();

		List<UserOnline> onlineUsers = sessionService.getOnlineUsers(request);
		if (ObjectUtil.isNotNull(onlineUsers)){
			result.setSuccess(true);
			result.setData(onlineUsers);
		}else {
			result.setSuccess(false);
			result.setMsg("数据为空");

		}

		return result;

	}

	@GetMapping("/kickOut")
	@ResponseBody
	public AjaxResult kickOutUser( String id){

		AjaxResult result = new AjaxResult();
		if (sessionService.kickOut(id)){
			result.setSuccess(true);
			result.setMsg("踢出成功");

		}else {
			result.setSuccess(false);
			result.setMsg("踢出失败");
		}

		return result;
	}




}
